package xj.graph2d.template.uml;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import xj.graph2d.DrawShape;
import xj.graph2d.shapes.uml.ClassShape;
import xj.graph2d.template.UMLTemplate;
import xj.model.uml.Enumeration;
import xj.model.uml.Value;

public class UMLEnumModelListener 
  extends UMLAbstractModelListener {

  public UMLEnumModelListener(Map iconMap, UMLTemplate template) {
    this(null, null, iconMap, template);
  }

  public UMLEnumModelListener(Object model, 
			      DrawShape shape, 
			      Map iconMap,
			      UMLTemplate template) {
    super(model, shape, iconMap, template);
  }

  @Override
  public void updateModel() {
    if (shape instanceof ClassShape && 
	model instanceof Enumeration) {
      if (debug) { 
	System.out.println("UMLEnumModelLisener.updateModel(): shape=" + shape);
      }

      ClassShape enumNode = (ClassShape) shape;
      Enumeration enumModel = (Enumeration) model;
      String name = enumNode.getClassName();
      String st = enumNode.getClassStereotypes();
      if (name != null) {
	enumModel.setName(name);
      }
      enumModel.setStereotypes(st);

      if (enumNode.isAttrOperCompartmentShown()) {
	enumModel.removeAllValues();
	List<Value> values = enumNode.getEnumValues();
	if (values != null) {
	  Iterator<Value> iter = values.iterator();
	  while (iter.hasNext()) {
	    Value v = iter.next();
	    enumModel.addValue(v);
	  }
	}
      }
    }
  }

  @Override
  public void updateShape() {
    if (shape instanceof ClassShape && 
	model instanceof Enumeration) {
      if (debug) { 
	System.out.println("UMLEnumModelLisener.updateShape(): shape=" + shape);
      }

      ClassShape enumNode = (ClassShape) shape;
      Enumeration enumModel = (Enumeration) model;
      String name = enumModel.getName();
      enumNode.setClassName(name);
      String st = enumModel.getStereotypeString();
      enumNode.setClassStereotypes(st);

      if (enumNode.isAttrOperCompartmentShown()) {
	List<Value> values = enumModel.getValues();
	enumNode.setEnumValues(values);
      }
    }
  }

}
